﻿using Creator.Core;
using Creator.Core.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Creator;

namespace DesktopCreator.Controls
{
    /// <summary>
    /// UCActions.xaml 的交互逻辑
    /// </summary>
    public partial class UCActions : UserControl
    {
        public UCActions()
        {
            InitializeComponent();
            btnAddAction.Click += BtnAddAction_Click;
        }


        private BaseObject _object;
        public UCActions(List<IAction> actions,BaseObject obj) : this()
        {
            _object = obj;
            foreach (var action in actions)
            { 
                gridActions.Children.Add(GetUCUCAction(action)); 
            }
            dlAction.OnCreate += (s) =>
            {
                if(s is IAction action)
                {
                    actions.Add(action);
                    gridActions.Children.Add(GetUCUCAction(action));
                }
                gridActions.ToggleUIVisable();
            };
        }

        /// <summary>
        /// 创建组件UI
        /// </summary>
        /// <param name="behaviour"></param>
        /// <returns></returns>
        private UCAction GetUCUCAction(IAction action)
        {
            var bh = new UCAction(action);
            //从主界面移除时
            bh.OnRemoved += (ebh) =>
            {
                gridActions.Children.Remove(ebh);
            };
            return bh;
        }
        private void BtnAddAction_Click(object sender, RoutedEventArgs e)
        {
            dlAction.ToggleUIVisable();
            gridActions.ToggleUIVisable();
            dlAction.CreateTpyeName(typeof(IAction), _object);
        }
    }
}
